<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* 
      思路：双指针 让fast先走n步
      */
      var removeNthFromEnd = function (head, n) {
        let fast = head
        let slow = head
        while (n) {
          n--
          fast = fast.next
        }
        if (!fast) return slow.next
        //为什么需要fast.next因为我们需要循环结束后fast再链表最后一个节点，而不是空节点
        while (fast && fast.next) {
          fast = fast.next
          slow = slow.next
        }
        let next = slow.next.next
        slow.next = next
        return head
      }
    </script>
  </body>
</html>
